package com.hjq.mallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hjq.mallproduct.entity.BrandEntity;
import com.hjq.mallproduct.entity.CategoryEntity;
import com.hjq.mallproduct.service.BrandService;
import com.hjq.mallproduct.service.CategoryService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.Query;

import com.hjq.mallproduct.dao.CategoryBrandRelationDao;
import com.hjq.mallproduct.entity.CategoryBrandRelationEntity;
import com.hjq.mallproduct.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    @Lazy
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getcateloglistByBrandId(Long brandId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntity = baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));

        return categoryBrandRelationEntity;
    }
    @Override
    public List<BrandEntity> getBrandlistByCatId(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntity = baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<Long> brandIdList = categoryBrandRelationEntity.stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
        List<BrandEntity> brandEntities = brandService.getBaseMapper().selectBatchIds(brandIdList);

        return brandEntities ;
    }

    @Override
    public void save(Long brandId, Long categoryId) {
        CategoryEntity categoryEntity = categoryService.getById(categoryId);
        BrandEntity brandEntity = brandService.getById(brandId);
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(brandEntity.getName());
        categoryBrandRelationEntity.setCatelogId(categoryId);
        categoryBrandRelationEntity.setCatelogName(categoryEntity.getName());
        baseMapper.insert(categoryBrandRelationEntity);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        baseMapper.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));


    }

    @Override
    public void updareCategory(Long catId, String categoryName) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(catId);
        categoryBrandRelationEntity.setCatelogName(categoryName);
        baseMapper.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));

    }

}